This is a project on conducting PCA (Principal Component Analysis) On Credit Card Transaction Data. The goal of PCA is to reduce the dimensionality/features of a dataset to capture the maximum variance in the least number of features. We find that using PCA, we can capture the statistically significant portion of the data in 4 Principal Components, down from 31 features. Original dataset can be found here: https://www.kaggle.com/datasets/mlg-ulb/creditcardfraud

Install packages

install.packages('tidyverse', repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Steve/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'tidyverse' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\Steve\AppData\Local\Temp\RtmpyWFmpx\downloaded_packages
library(tidyverse)
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0      ✔ purrr   0.3.5 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.3      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()

Read CSV

credit_df <- read_csv('creditcard.csv')
## Rows: 284807 Columns: 31
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (31): Time, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14,...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Structure

str(credit_df)
## spc_tbl_ [284,807 × 31] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ Time  : num [1:284807] 0 0 1 1 2 2 4 7 7 9 ...
##  $ V1    : num [1:284807] -1.36 1.192 -1.358 -0.966 -1.158 ...
##  $ V2    : num [1:284807] -0.0728 0.2662 -1.3402 -0.1852 0.8777 ...
##  $ V3    : num [1:284807] 2.536 0.166 1.773 1.793 1.549 ...
##  $ V4    : num [1:284807] 1.378 0.448 0.38 -0.863 0.403 ...
##  $ V5    : num [1:284807] -0.3383 0.06 -0.5032 -0.0103 -0.4072 ...
##  $ V6    : num [1:284807] 0.4624 -0.0824 1.8005 1.2472 0.0959 ...
##  $ V7    : num [1:284807] 0.2396 -0.0788 0.7915 0.2376 0.5929 ...
##  $ V8    : num [1:284807] 0.0987 0.0851 0.2477 0.3774 -0.2705 ...
##  $ V9    : num [1:284807] 0.364 -0.255 -1.515 -1.387 0.818 ...
##  $ V10   : num [1:284807] 0.0908 -0.167 0.2076 -0.055 0.7531 ...
##  $ V11   : num [1:284807] -0.552 1.613 0.625 -0.226 -0.823 ...
##  $ V12   : num [1:284807] -0.6178 1.0652 0.0661 0.1782 0.5382 ...
##  $ V13   : num [1:284807] -0.991 0.489 0.717 0.508 1.346 ...
##  $ V14   : num [1:284807] -0.311 -0.144 -0.166 -0.288 -1.12 ...
##  $ V15   : num [1:284807] 1.468 0.636 2.346 -0.631 0.175 ...
##  $ V16   : num [1:284807] -0.47 0.464 -2.89 -1.06 -0.451 ...
##  $ V17   : num [1:284807] 0.208 -0.115 1.11 -0.684 -0.237 ...
##  $ V18   : num [1:284807] 0.0258 -0.1834 -0.1214 1.9658 -0.0382 ...
##  $ V19   : num [1:284807] 0.404 -0.146 -2.262 -1.233 0.803 ...
##  $ V20   : num [1:284807] 0.2514 -0.0691 0.525 -0.208 0.4085 ...
##  $ V21   : num [1:284807] -0.01831 -0.22578 0.248 -0.1083 -0.00943 ...
##  $ V22   : num [1:284807] 0.27784 -0.63867 0.77168 0.00527 0.79828 ...
##  $ V23   : num [1:284807] -0.11 0.101 0.909 -0.19 -0.137 ...
##  $ V24   : num [1:284807] 0.0669 -0.3398 -0.6893 -1.1756 0.1413 ...
##  $ V25   : num [1:284807] 0.129 0.167 -0.328 0.647 -0.206 ...
##  $ V26   : num [1:284807] -0.189 0.126 -0.139 -0.222 0.502 ...
##  $ V27   : num [1:284807] 0.13356 -0.00898 -0.05535 0.06272 0.21942 ...
##  $ V28   : num [1:284807] -0.0211 0.0147 -0.0598 0.0615 0.2152 ...
##  $ Amount: num [1:284807] 149.62 2.69 378.66 123.5 69.99 ...
##  $ Class : num [1:284807] 0 0 0 0 0 0 0 0 0 0 ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   Time = col_double(),
##   ..   V1 = col_double(),
##   ..   V2 = col_double(),
##   ..   V3 = col_double(),
##   ..   V4 = col_double(),
##   ..   V5 = col_double(),
##   ..   V6 = col_double(),
##   ..   V7 = col_double(),
##   ..   V8 = col_double(),
##   ..   V9 = col_double(),
##   ..   V10 = col_double(),
##   ..   V11 = col_double(),
##   ..   V12 = col_double(),
##   ..   V13 = col_double(),
##   ..   V14 = col_double(),
##   ..   V15 = col_double(),
##   ..   V16 = col_double(),
##   ..   V17 = col_double(),
##   ..   V18 = col_double(),
##   ..   V19 = col_double(),
##   ..   V20 = col_double(),
##   ..   V21 = col_double(),
##   ..   V22 = col_double(),
##   ..   V23 = col_double(),
##   ..   V24 = col_double(),
##   ..   V25 = col_double(),
##   ..   V26 = col_double(),
##   ..   V27 = col_double(),
##   ..   V28 = col_double(),
##   ..   Amount = col_double(),
##   ..   Class = col_double()
##   .. )
##  - attr(*, "problems")=<externalptr>
summary(credit_df)
##       Time              V1                  V2                  V3          
##  Min.   :     0   Min.   :-56.40751   Min.   :-72.71573   Min.   :-48.3256  
##  1st Qu.: 54202   1st Qu.: -0.92037   1st Qu.: -0.59855   1st Qu.: -0.8904  
##  Median : 84692   Median :  0.01811   Median :  0.06549   Median :  0.1799  
##  Mean   : 94814   Mean   :  0.00000   Mean   :  0.00000   Mean   :  0.0000  
##  3rd Qu.:139321   3rd Qu.:  1.31564   3rd Qu.:  0.80372   3rd Qu.:  1.0272  
##  Max.   :172792   Max.   :  2.45493   Max.   : 22.05773   Max.   :  9.3826  
##        V4                 V5                   V6                 V7          
##  Min.   :-5.68317   Min.   :-113.74331   Min.   :-26.1605   Min.   :-43.5572  
##  1st Qu.:-0.84864   1st Qu.:  -0.69160   1st Qu.: -0.7683   1st Qu.: -0.5541  
##  Median :-0.01985   Median :  -0.05434   Median : -0.2742   Median :  0.0401  
##  Mean   : 0.00000   Mean   :   0.00000   Mean   :  0.0000   Mean   :  0.0000  
##  3rd Qu.: 0.74334   3rd Qu.:   0.61193   3rd Qu.:  0.3986   3rd Qu.:  0.5704  
##  Max.   :16.87534   Max.   :  34.80167   Max.   : 73.3016   Max.   :120.5895  
##        V8                  V9                 V10                 V11          
##  Min.   :-73.21672   Min.   :-13.43407   Min.   :-24.58826   Min.   :-4.79747  
##  1st Qu.: -0.20863   1st Qu.: -0.64310   1st Qu.: -0.53543   1st Qu.:-0.76249  
##  Median :  0.02236   Median : -0.05143   Median : -0.09292   Median :-0.03276  
##  Mean   :  0.00000   Mean   :  0.00000   Mean   :  0.00000   Mean   : 0.00000  
##  3rd Qu.:  0.32735   3rd Qu.:  0.59714   3rd Qu.:  0.45392   3rd Qu.: 0.73959  
##  Max.   : 20.00721   Max.   : 15.59500   Max.   : 23.74514   Max.   :12.01891  
##       V12                V13                V14                V15          
##  Min.   :-18.6837   Min.   :-5.79188   Min.   :-19.2143   Min.   :-4.49894  
##  1st Qu.: -0.4056   1st Qu.:-0.64854   1st Qu.: -0.4256   1st Qu.:-0.58288  
##  Median :  0.1400   Median :-0.01357   Median :  0.0506   Median : 0.04807  
##  Mean   :  0.0000   Mean   : 0.00000   Mean   :  0.0000   Mean   : 0.00000  
##  3rd Qu.:  0.6182   3rd Qu.: 0.66251   3rd Qu.:  0.4931   3rd Qu.: 0.64882  
##  Max.   :  7.8484   Max.   : 7.12688   Max.   : 10.5268   Max.   : 8.87774  
##       V16                 V17                 V18           
##  Min.   :-14.12985   Min.   :-25.16280   Min.   :-9.498746  
##  1st Qu.: -0.46804   1st Qu.: -0.48375   1st Qu.:-0.498850  
##  Median :  0.06641   Median : -0.06568   Median :-0.003636  
##  Mean   :  0.00000   Mean   :  0.00000   Mean   : 0.000000  
##  3rd Qu.:  0.52330   3rd Qu.:  0.39968   3rd Qu.: 0.500807  
##  Max.   : 17.31511   Max.   :  9.25353   Max.   : 5.041069  
##       V19                 V20                 V21           
##  Min.   :-7.213527   Min.   :-54.49772   Min.   :-34.83038  
##  1st Qu.:-0.456299   1st Qu.: -0.21172   1st Qu.: -0.22839  
##  Median : 0.003735   Median : -0.06248   Median : -0.02945  
##  Mean   : 0.000000   Mean   :  0.00000   Mean   :  0.00000  
##  3rd Qu.: 0.458949   3rd Qu.:  0.13304   3rd Qu.:  0.18638  
##  Max.   : 5.591971   Max.   : 39.42090   Max.   : 27.20284  
##       V22                  V23                 V24          
##  Min.   :-10.933144   Min.   :-44.80774   Min.   :-2.83663  
##  1st Qu.: -0.542350   1st Qu.: -0.16185   1st Qu.:-0.35459  
##  Median :  0.006782   Median : -0.01119   Median : 0.04098  
##  Mean   :  0.000000   Mean   :  0.00000   Mean   : 0.00000  
##  3rd Qu.:  0.528554   3rd Qu.:  0.14764   3rd Qu.: 0.43953  
##  Max.   : 10.503090   Max.   : 22.52841   Max.   : 4.58455  
##       V25                 V26                V27            
##  Min.   :-10.29540   Min.   :-2.60455   Min.   :-22.565679  
##  1st Qu.: -0.31715   1st Qu.:-0.32698   1st Qu.: -0.070840  
##  Median :  0.01659   Median :-0.05214   Median :  0.001342  
##  Mean   :  0.00000   Mean   : 0.00000   Mean   :  0.000000  
##  3rd Qu.:  0.35072   3rd Qu.: 0.24095   3rd Qu.:  0.091045  
##  Max.   :  7.51959   Max.   : 3.51735   Max.   : 31.612198  
##       V28                Amount             Class         
##  Min.   :-15.43008   Min.   :    0.00   Min.   :0.000000  
##  1st Qu.: -0.05296   1st Qu.:    5.60   1st Qu.:0.000000  
##  Median :  0.01124   Median :   22.00   Median :0.000000  
##  Mean   :  0.00000   Mean   :   88.35   Mean   :0.001728  
##  3rd Qu.:  0.07828   3rd Qu.:   77.17   3rd Qu.:0.000000  
##  Max.   : 33.84781   Max.   :25691.16   Max.   :1.000000

Filter Class not equal to 0

Class can only be 0 or 1

view(filter(credit_df, Class != 0))

Create correlation matrix to check PCA eligibility

Average correlation is 0.0259

round(cor(credit_df),3)
##          Time     V1     V2     V3     V4     V5     V6     V7     V8     V9
## Time    1.000  0.117 -0.011 -0.420 -0.105  0.173 -0.063  0.085 -0.037 -0.009
## V1      0.117  1.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V2     -0.011  0.000  1.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V3     -0.420  0.000  0.000  1.000  0.000  0.000  0.000  0.000  0.000  0.000
## V4     -0.105  0.000  0.000  0.000  1.000  0.000  0.000  0.000  0.000  0.000
## V5      0.173  0.000  0.000  0.000  0.000  1.000  0.000  0.000  0.000  0.000
## V6     -0.063  0.000  0.000  0.000  0.000  0.000  1.000  0.000  0.000  0.000
## V7      0.085  0.000  0.000  0.000  0.000  0.000  0.000  1.000  0.000  0.000
## V8     -0.037  0.000  0.000  0.000  0.000  0.000  0.000  0.000  1.000  0.000
## V9     -0.009  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  1.000
## V10     0.031  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V11    -0.248  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V12     0.124  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V13    -0.066  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V14    -0.099  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V15    -0.183  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V16     0.012  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V17    -0.073  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V18     0.090  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V19     0.029  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V20    -0.051  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V21     0.045  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V22     0.144  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V23     0.051  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V24    -0.016  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V25    -0.233  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V26    -0.041  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V27    -0.005  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V28    -0.009  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## Amount -0.011 -0.228 -0.531 -0.211  0.099 -0.386  0.216  0.397 -0.103 -0.044
## Class  -0.012 -0.101  0.091 -0.193  0.133 -0.095 -0.044 -0.187  0.020 -0.098
##           V10    V11    V12    V13    V14    V15    V16    V17    V18    V19
## Time    0.031 -0.248  0.124 -0.066 -0.099 -0.183  0.012 -0.073  0.090  0.029
## V1      0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V2      0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V3      0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V4      0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V5      0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V6      0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V7      0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V8      0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V9      0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V10     1.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V11     0.000  1.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V12     0.000  0.000  1.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V13     0.000  0.000  0.000  1.000  0.000  0.000  0.000  0.000  0.000  0.000
## V14     0.000  0.000  0.000  0.000  1.000  0.000  0.000  0.000  0.000  0.000
## V15     0.000  0.000  0.000  0.000  0.000  1.000  0.000  0.000  0.000  0.000
## V16     0.000  0.000  0.000  0.000  0.000  0.000  1.000  0.000  0.000  0.000
## V17     0.000  0.000  0.000  0.000  0.000  0.000  0.000  1.000  0.000  0.000
## V18     0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  1.000  0.000
## V19     0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  1.000
## V20     0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V21     0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V22     0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V23     0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V24     0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V25     0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V26     0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V27     0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V28     0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## Amount -0.102  0.000 -0.010  0.005  0.034 -0.003 -0.004  0.007  0.036 -0.056
## Class  -0.217  0.155 -0.261 -0.005 -0.303 -0.004 -0.197 -0.326 -0.111  0.035
##           V20   V21    V22    V23    V24    V25    V26    V27    V28 Amount
## Time   -0.051 0.045  0.144  0.051 -0.016 -0.233 -0.041 -0.005 -0.009 -0.011
## V1      0.000 0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000 -0.228
## V2      0.000 0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000 -0.531
## V3      0.000 0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000 -0.211
## V4      0.000 0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.099
## V5      0.000 0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000 -0.386
## V6      0.000 0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.216
## V7      0.000 0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.397
## V8      0.000 0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000 -0.103
## V9      0.000 0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000 -0.044
## V10     0.000 0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000 -0.102
## V11     0.000 0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
## V12     0.000 0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000 -0.010
## V13     0.000 0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.005
## V14     0.000 0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.034
## V15     0.000 0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000 -0.003
## V16     0.000 0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000 -0.004
## V17     0.000 0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.007
## V18     0.000 0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.036
## V19     0.000 0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000 -0.056
## V20     1.000 0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.339
## V21     0.000 1.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.106
## V22     0.000 0.000  1.000  0.000  0.000  0.000  0.000  0.000  0.000 -0.065
## V23     0.000 0.000  0.000  1.000  0.000  0.000  0.000  0.000  0.000 -0.113
## V24     0.000 0.000  0.000  0.000  1.000  0.000  0.000  0.000  0.000  0.005
## V25     0.000 0.000  0.000  0.000  0.000  1.000  0.000  0.000  0.000 -0.048
## V26     0.000 0.000  0.000  0.000  0.000  0.000  1.000  0.000  0.000 -0.003
## V27     0.000 0.000  0.000  0.000  0.000  0.000  0.000  1.000  0.000  0.029
## V28     0.000 0.000  0.000  0.000  0.000  0.000  0.000  0.000  1.000  0.010
## Amount  0.339 0.106 -0.065 -0.113  0.005 -0.048 -0.003  0.029  0.010  1.000
## Class   0.020 0.040  0.001 -0.003 -0.007  0.003  0.004  0.018  0.010  0.006
##         Class
## Time   -0.012
## V1     -0.101
## V2      0.091
## V3     -0.193
## V4      0.133
## V5     -0.095
## V6     -0.044
## V7     -0.187
## V8      0.020
## V9     -0.098
## V10    -0.217
## V11     0.155
## V12    -0.261
## V13    -0.005
## V14    -0.303
## V15    -0.004
## V16    -0.197
## V17    -0.326
## V18    -0.111
## V19     0.035
## V20     0.020
## V21     0.040
## V22     0.001
## V23    -0.003
## V24    -0.007
## V25     0.003
## V26     0.004
## V27     0.018
## V28     0.010
## Amount  0.006
## Class   1.000
mean(cor(credit_df)) 
## [1] 0.0259556

Conduct PCA on scaled correlation matrix since we have data on different scales

We use PCA on covariance matrix when data is on same scale

PCA = princomp(credit_df, cor=TRUE, scale=TRUE)
## Warning: In princomp.default(credit_df, cor = TRUE, scale = TRUE) :
##  extra argument 'scale' will be disregarded
print(PCA)
## Call:
## princomp(x = credit_df, cor = TRUE, scale = TRUE)
## 
## Standard deviations:
##    Comp.1    Comp.2    Comp.3    Comp.4    Comp.5    Comp.6    Comp.7    Comp.8 
## 1.3994101 1.3137433 1.2960734 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 
##    Comp.9   Comp.10   Comp.11   Comp.12   Comp.13   Comp.14   Comp.15   Comp.16 
## 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 
##   Comp.17   Comp.18   Comp.19   Comp.20   Comp.21   Comp.22   Comp.23   Comp.24 
## 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 
##   Comp.25   Comp.26   Comp.27   Comp.28   Comp.29   Comp.30   Comp.31 
## 1.0000000 1.0000000 1.0000000 1.0000000 0.5620611 0.5271344 0.2052813 
## 
##  31  variables and  284807 observations.

Summary of PCA

summary(PCA)
## Importance of components:
##                            Comp.1     Comp.2    Comp.3     Comp.4     Comp.5
## Standard deviation     1.39941006 1.31374332 1.2960734 1.00000000 1.00000000
## Proportion of Variance 0.06317253 0.05567489 0.0541873 0.03225806 0.03225806
## Cumulative Proportion  0.06317253 0.11884742 0.1730347 0.20529278 0.23755085
##                            Comp.6     Comp.7     Comp.8     Comp.9    Comp.10
## Standard deviation     1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## Proportion of Variance 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806
## Cumulative Proportion  0.26980891 0.30206698 0.33432504 0.36658310 0.39884117
##                           Comp.11    Comp.12    Comp.13    Comp.14    Comp.15
## Standard deviation     1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## Proportion of Variance 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806
## Cumulative Proportion  0.43109923 0.46335730 0.49561536 0.52787343 0.56013149
##                           Comp.16    Comp.17    Comp.18    Comp.19    Comp.20
## Standard deviation     1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## Proportion of Variance 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806
## Cumulative Proportion  0.59238956 0.62464762 0.65690569 0.68916375 0.72142181
##                           Comp.21    Comp.22    Comp.23    Comp.24    Comp.25
## Standard deviation     1.00000000 1.00000000 1.00000000 1.00000000 1.00000000
## Proportion of Variance 0.03225806 0.03225806 0.03225806 0.03225806 0.03225806
## Cumulative Proportion  0.75367988 0.78593794 0.81819601 0.85045407 0.88271214
##                           Comp.26    Comp.27    Comp.28    Comp.29    Comp.30
## Standard deviation     1.00000000 1.00000000 1.00000000 0.56206107 0.52713441
## Proportion of Variance 0.03225806 0.03225806 0.03225806 0.01019073 0.00896357
## Cumulative Proportion  0.91497020 0.94722827 0.97948633 0.98967706 0.99864063
##                            Comp.31
## Standard deviation     0.205281338
## Proportion of Variance 0.001359369
## Cumulative Proportion  1.000000000

names

names(PCA)
## [1] "sdev"     "loadings" "center"   "scale"    "n.obs"    "scores"   "call"

Use loading matrix to evaluate if PCA captures the essence of original variables

Loadings show the eigenvectors of the correlation matrix

Eigen_vectors <- PCA$loadings
Eigen_vectors
## 
## Loadings:
##        Comp.1 Comp.2 Comp.3 Comp.4 Comp.5 Comp.6 Comp.7 Comp.8 Comp.9 Comp.10
## Time           0.207  0.675                                                  
## V1      0.173  0.122         0.127                0.105                0.101 
## V2      0.390 -0.102               -0.108        -0.111                0.314 
## V3      0.145        -0.481        -0.183        -0.311  0.273  0.134        
## V4            -0.152        -0.119                0.173                      
## V5      0.292  0.128  0.132                0.310 -0.138  0.151 -0.102        
## V6     -0.161                              0.254 -0.371 -0.164  0.130        
## V7     -0.287  0.210               -0.129  0.112         0.187 -0.124  0.580 
## V8                                         0.131         0.197 -0.157        
## V9                           0.121 -0.123 -0.162  0.295                0.228 
## V10            0.208                      -0.212        -0.172 -0.129 -0.350 
## V11           -0.215 -0.200         0.122 -0.199  0.105  0.389 -0.312        
## V12            0.278        -0.622                                           
## V13                          0.684                                           
## V14            0.255 -0.187  0.114        -0.283                             
## V15                  -0.184        -0.141  0.164 -0.116 -0.390 -0.442 -0.170 
## V16            0.187                0.265  0.206 -0.217 -0.135        -0.200 
## V17            0.284 -0.170  0.150  0.441         0.217                      
## V18            0.131               -0.665                                    
## V19                                 0.357        -0.194         0.261  0.276 
## V20    -0.252                              0.138 -0.136                      
## V21                                       -0.491 -0.454        -0.104        
## V22                   0.141               -0.426 -0.419  0.123 -0.125        
## V23                                 0.129                       0.301        
## V24                                       -0.159  0.138                      
## V25                  -0.232               -0.110        -0.394  0.205  0.128 
## V26                                       -0.105        -0.333  0.115  0.271 
## V27                                        0.157         0.336  0.314 -0.283 
## V28                                -0.107 -0.119         0.102  0.468 -0.139 
## Amount -0.707                                                                
## Class         -0.677  0.202                                                  
##        Comp.11 Comp.12 Comp.13 Comp.14 Comp.15 Comp.16 Comp.17 Comp.18 Comp.19
## Time                                                                          
## V1             -0.105  -0.114   0.112  -0.415  -0.479  -0.338  -0.136         
## V2     -0.167  -0.198          -0.317          -0.265   0.133   0.167         
## V3     -0.178           0.152           0.115                                 
## V4              0.360          -0.209  -0.105          -0.346   0.301   0.333 
## V5      0.367   0.321  -0.244   0.260           0.201                  -0.149 
## V6                     -0.430   0.131          -0.247  -0.294           0.184 
## V7                                                              0.242         
## V8              0.363   0.333  -0.141  -0.325   0.207  -0.400          -0.100 
## V9     -0.108   0.336   0.264   0.237   0.370  -0.344          -0.308         
## V10    -0.149  -0.163   0.130   0.168           0.105  -0.295   0.224         
## V11     0.355  -0.149  -0.147   0.256          -0.188  -0.167                 
## V12                                                                           
## V13                                                             0.100         
## V14     0.137          -0.374  -0.300  -0.191                   0.159         
## V15     0.227   0.143          -0.198   0.123  -0.222                   0.102 
## V16                     0.403   0.107  -0.139  -0.177   0.225   0.189   0.183 
## V17                            -0.189                          -0.165   0.119 
## V18            -0.129           0.156           0.145  -0.186           0.122 
## V19    -0.221                   0.262                  -0.280   0.193         
## V20            -0.292   0.239          -0.339  -0.198          -0.122  -0.438 
## V21             0.297                                                  -0.129 
## V22                            -0.173  -0.164                  -0.208   0.245 
## V23     0.465  -0.113   0.288  -0.131   0.256  -0.130  -0.243   0.325  -0.135 
## V24     0.115  -0.132           0.429  -0.268           0.257   0.258   0.415 
## V25             0.218           0.216  -0.386           0.123                 
## V26     0.454  -0.206          -0.108           0.266  -0.117  -0.397   0.184 
## V27                     0.105                  -0.153          -0.298   0.422 
## V28     0.181   0.252                          -0.313   0.206   0.128  -0.197 
## Amount                                                                        
## Class                                                                         
##        Comp.20 Comp.21 Comp.22 Comp.23 Comp.24 Comp.25 Comp.26 Comp.27 Comp.28
## Time                                                                          
## V1     -0.159  -0.397   0.113          -0.230  -0.114                         
## V2      0.274   0.226  -0.181           0.148                  -0.253         
## V3     -0.218  -0.112           0.133  -0.190                   0.129         
## V4     -0.483   0.145  -0.132   0.140          -0.170          -0.141         
## V5     -0.237   0.203                   0.151  -0.195                         
## V6      0.218   0.341  -0.176          -0.245   0.129   0.105                 
## V7      0.141  -0.243  -0.140  -0.213          -0.138   0.194   0.127         
## V8      0.505                          -0.102   0.156                         
## V9              0.274   0.142           0.136   0.217   0.101                 
## V10     0.156          -0.251           0.186  -0.326   0.370   0.170         
## V11                    -0.270           0.180   0.180                   0.206 
## V12                                                                     0.660 
## V13                                                                     0.679 
## V14                     0.339           0.249   0.289   0.177  -0.174  -0.164 
## V15                     0.326           0.179  -0.160  -0.200   0.261         
## V16            -0.155  -0.239   0.249   0.151   0.312          -0.257         
## V17             0.172  -0.186  -0.125  -0.139  -0.225  -0.508                 
## V18                    -0.117           0.131   0.112  -0.520  -0.221         
## V19            -0.139   0.250           0.454          -0.272   0.226         
## V20    -0.177   0.410                   0.235  -0.201                         
## V21     0.131  -0.117   0.114                  -0.337  -0.109  -0.456         
## V22    -0.227   0.191  -0.152  -0.146           0.212           0.453         
## V23    -0.106           0.170  -0.270  -0.364   0.107                         
## V24     0.164   0.345   0.299   0.138  -0.228  -0.158                         
## V25                    -0.217  -0.549                                         
## V26     0.108          -0.110   0.393          -0.156   0.142                 
## V27     0.134           0.209  -0.335   0.270  -0.240   0.183  -0.143         
## V28     0.167          -0.250   0.309          -0.233  -0.201   0.351         
## Amount                                                                        
## Class                                                                         
##        Comp.29 Comp.30 Comp.31
## Time    0.707                 
## V1     -0.118  -0.102   0.167 
## V2                      0.393 
## V3      0.439  -0.185   0.155 
## V4      0.106   0.132         
## V5     -0.175           0.284 
## V6                     -0.160 
## V7             -0.182  -0.295 
## V8                            
## V9                            
## V10            -0.213         
## V11     0.253   0.155         
## V12    -0.123  -0.257         
## V13                           
## V14     0.109  -0.295         
## V15     0.190                 
## V16            -0.193         
## V17            -0.319         
## V18            -0.110         
## V19                           
## V20                    -0.250 
## V21                           
## V22    -0.149                 
## V23                           
## V24                           
## V25     0.241                 
## V26                           
## V27                           
## V28                           
## Amount                  0.707 
## Class          -0.707         
## 
##                Comp.1 Comp.2 Comp.3 Comp.4 Comp.5 Comp.6 Comp.7 Comp.8 Comp.9
## SS loadings     1.000  1.000  1.000  1.000  1.000  1.000  1.000  1.000  1.000
## Proportion Var  0.032  0.032  0.032  0.032  0.032  0.032  0.032  0.032  0.032
## Cumulative Var  0.032  0.065  0.097  0.129  0.161  0.194  0.226  0.258  0.290
##                Comp.10 Comp.11 Comp.12 Comp.13 Comp.14 Comp.15 Comp.16 Comp.17
## SS loadings      1.000   1.000   1.000   1.000   1.000   1.000   1.000   1.000
## Proportion Var   0.032   0.032   0.032   0.032   0.032   0.032   0.032   0.032
## Cumulative Var   0.323   0.355   0.387   0.419   0.452   0.484   0.516   0.548
##                Comp.18 Comp.19 Comp.20 Comp.21 Comp.22 Comp.23 Comp.24 Comp.25
## SS loadings      1.000   1.000   1.000   1.000   1.000   1.000   1.000   1.000
## Proportion Var   0.032   0.032   0.032   0.032   0.032   0.032   0.032   0.032
## Cumulative Var   0.581   0.613   0.645   0.677   0.710   0.742   0.774   0.806
##                Comp.26 Comp.27 Comp.28 Comp.29 Comp.30 Comp.31
## SS loadings      1.000   1.000   1.000   1.000   1.000   1.000
## Proportion Var   0.032   0.032   0.032   0.032   0.032   0.032
## Cumulative Var   0.839   0.871   0.903   0.935   0.968   1.000

Eigen values are standard dev squared

Eigen_values <- PCA$sdev * PCA$sdev 
Eigen_values
##     Comp.1     Comp.2     Comp.3     Comp.4     Comp.5     Comp.6     Comp.7 
## 1.95834852 1.72592150 1.67980622 1.00000000 1.00000000 1.00000000 1.00000000 
##     Comp.8     Comp.9    Comp.10    Comp.11    Comp.12    Comp.13    Comp.14 
## 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 
##    Comp.15    Comp.16    Comp.17    Comp.18    Comp.19    Comp.20    Comp.21 
## 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 
##    Comp.22    Comp.23    Comp.24    Comp.25    Comp.26    Comp.27    Comp.28 
## 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 1.00000000 
##    Comp.29    Comp.30    Comp.31 
## 0.31591265 0.27787068 0.04214043

Create correlation matrix between original data and Scores

Scores are the values projected onto the eigenvectors

round(cor(credit_df, PCA$scores), 3)
##        Comp.1 Comp.2 Comp.3 Comp.4 Comp.5 Comp.6 Comp.7 Comp.8 Comp.9 Comp.10
## Time    0.042  0.272  0.875  0.000  0.000  0.000  0.000  0.000  0.000   0.000
## V1      0.242  0.160  0.103  0.127 -0.069 -0.034  0.105 -0.071 -0.076   0.101
## V2      0.546 -0.135  0.000 -0.069 -0.108  0.028 -0.111 -0.023 -0.020   0.314
## V3      0.203  0.072 -0.623 -0.097 -0.183  0.050 -0.311  0.273  0.134   0.089
## V4     -0.109 -0.200 -0.080 -0.119 -0.033 -0.070  0.173 -0.020  0.062   0.087
## V5      0.408  0.168  0.171  0.087  0.038  0.310 -0.138  0.151 -0.102   0.094
## V6     -0.225  0.037 -0.089  0.019 -0.020  0.254 -0.371 -0.164  0.130  -0.058
## V7     -0.402  0.275  0.053  0.044 -0.129  0.112 -0.062  0.187 -0.124   0.580
## V8      0.104 -0.042 -0.044 -0.020  0.009  0.131 -0.014  0.197 -0.157   0.003
## V9      0.047  0.115 -0.051  0.121 -0.123 -0.162  0.295 -0.095 -0.011   0.228
## V10     0.111  0.274 -0.048  0.064 -0.051 -0.212  0.080 -0.172 -0.129  -0.350
## V11    -0.014 -0.283 -0.259 -0.003  0.122 -0.199  0.105  0.389 -0.312  -0.003
## V12     0.021  0.366  0.059 -0.622  0.013 -0.043  0.013  0.010  0.028  -0.014
## V13    -0.008 -0.019 -0.086  0.684 -0.042  0.050 -0.055 -0.076  0.076   0.015
## V14    -0.033  0.335 -0.242  0.114 -0.058 -0.283  0.060  0.074  0.096  -0.016
## V15    -0.005 -0.064 -0.238 -0.086 -0.141  0.164 -0.116 -0.390 -0.442  -0.170
## V16     0.009  0.245 -0.061  0.050  0.265  0.206 -0.217 -0.135 -0.042  -0.200
## V17    -0.004  0.373 -0.220  0.150  0.441 -0.007  0.217  0.068 -0.086   0.037
## V18    -0.030  0.172  0.075  0.050 -0.665  0.008 -0.038  0.005  0.085  -0.067
## V19     0.058 -0.034  0.048 -0.012  0.357 -0.009 -0.194 -0.097  0.261   0.276
## V20    -0.353 -0.032 -0.044 -0.040  0.003  0.138 -0.136  0.033 -0.038   0.080
## V21    -0.108 -0.029  0.077  0.006  0.084 -0.491 -0.454 -0.010 -0.104  -0.026
## V22     0.073  0.051  0.183  0.089 -0.022 -0.426 -0.419  0.123 -0.125  -0.007
## V23     0.119  0.019  0.060 -0.030  0.129 -0.080 -0.059 -0.055  0.301  -0.008
## V24    -0.006  0.003 -0.023 -0.069 -0.008 -0.159  0.138  0.044 -0.066   0.075
## V25     0.039 -0.093 -0.301 -0.084  0.012 -0.110  0.035 -0.394  0.205   0.128
## V26     0.001 -0.021 -0.052 -0.028  0.026 -0.105 -0.017 -0.333  0.115   0.271
## V27    -0.030 -0.022  0.001 -0.001  0.035  0.157 -0.019  0.336  0.314  -0.283
## V28    -0.011 -0.015 -0.008  0.024 -0.107 -0.119  0.087  0.102  0.468  -0.139
## Amount -0.989  0.026  0.027  0.000  0.000  0.000  0.000  0.000  0.000   0.000
## Class  -0.021 -0.890  0.262  0.000  0.000  0.000  0.000  0.000  0.000   0.000
##        Comp.11 Comp.12 Comp.13 Comp.14 Comp.15 Comp.16 Comp.17 Comp.18 Comp.19
## Time     0.000   0.000   0.000   0.000   0.000   0.000   0.000   0.000   0.000
## V1      -0.017  -0.105  -0.114   0.112  -0.415  -0.479  -0.338  -0.136   0.023
## V2      -0.167  -0.198   0.015  -0.317   0.000  -0.265   0.133   0.167   0.077
## V3      -0.178  -0.062   0.152   0.095   0.115   0.092   0.020  -0.068  -0.046
## V4      -0.096   0.360   0.031  -0.209  -0.105   0.040  -0.346   0.301   0.333
## V5       0.367   0.321  -0.244   0.260  -0.001   0.201   0.026  -0.007  -0.149
## V6      -0.042   0.049  -0.430   0.131   0.097  -0.247  -0.294  -0.038   0.184
## V7       0.088   0.086   0.071  -0.057   0.068  -0.066   0.072   0.242   0.086
## V8       0.016   0.363   0.333  -0.141  -0.325   0.207  -0.400  -0.039  -0.100
## V9      -0.108   0.336   0.264   0.237   0.370  -0.344  -0.020  -0.308  -0.038
## V10     -0.149  -0.163   0.130   0.168   0.098   0.105  -0.295   0.224  -0.084
## V11      0.355  -0.149  -0.147   0.256   0.097  -0.188  -0.167   0.068  -0.018
## V12     -0.020   0.032  -0.045  -0.020  -0.006  -0.025   0.018  -0.043  -0.038
## V13     -0.039   0.000   0.055  -0.053  -0.036   0.089   0.072   0.100   0.003
## V14      0.137  -0.001  -0.374  -0.300  -0.191   0.060   0.006   0.159  -0.075
## V15      0.227   0.143  -0.023  -0.198   0.123  -0.222   0.052   0.086   0.102
## V16      0.085   0.067   0.403   0.107  -0.139  -0.177   0.225   0.189   0.183
## V17     -0.037  -0.031  -0.041  -0.189   0.067   0.006  -0.049  -0.165   0.119
## V18      0.094  -0.129   0.085   0.156   0.027   0.145  -0.186   0.037   0.122
## V19     -0.221  -0.089  -0.039   0.262   0.060   0.055  -0.280   0.193   0.045
## V20      0.049  -0.292   0.239  -0.001  -0.339  -0.198  -0.078  -0.122  -0.438
## V21     -0.039   0.297   0.010   0.095   0.097  -0.031   0.014  -0.015  -0.129
## V22      0.030   0.002   0.082  -0.173  -0.164  -0.035   0.035  -0.208   0.245
## V23      0.465  -0.113   0.288  -0.131   0.256  -0.130  -0.243   0.325  -0.135
## V24      0.115  -0.132   0.081   0.429  -0.268   0.036   0.257   0.258   0.415
## V25      0.077   0.218  -0.033   0.216  -0.386   0.088   0.123  -0.089  -0.078
## V26      0.454  -0.206   0.088  -0.108   0.066   0.266  -0.117  -0.397   0.184
## V27      0.095   0.026   0.105  -0.040  -0.012  -0.153  -0.038  -0.298   0.422
## V28      0.181   0.252  -0.048  -0.052  -0.073  -0.313   0.206   0.128  -0.197
## Amount   0.000   0.000   0.000   0.000   0.000   0.000   0.000   0.000   0.000
## Class    0.000   0.000   0.000   0.000   0.000   0.000   0.000   0.000   0.000
##        Comp.20 Comp.21 Comp.22 Comp.23 Comp.24 Comp.25 Comp.26 Comp.27 Comp.28
## Time     0.000   0.000   0.000   0.000   0.000   0.000   0.000   0.000   0.000
## V1      -0.159  -0.397   0.113   0.084  -0.230  -0.114   0.033  -0.013  -0.015
## V2       0.274   0.226  -0.181  -0.057   0.148  -0.094   0.005  -0.253   0.021
## V3      -0.218  -0.112   0.078   0.133  -0.190  -0.100   0.067   0.129  -0.063
## V4      -0.483   0.145  -0.132   0.140   0.094  -0.170   0.057  -0.141   0.028
## V5      -0.237   0.203  -0.009  -0.018   0.151  -0.195   0.068  -0.021  -0.049
## V6       0.218   0.341  -0.176   0.080  -0.245   0.129   0.105   0.002   0.009
## V7       0.141  -0.243  -0.140  -0.213   0.007  -0.138   0.194   0.127  -0.043
## V8       0.505   0.051   0.066   0.097  -0.102   0.156  -0.061   0.098   0.032
## V9      -0.008   0.274   0.142   0.045   0.136   0.217   0.101  -0.017  -0.014
## V10      0.156   0.078  -0.251  -0.100   0.186  -0.326   0.370   0.170  -0.045
## V11      0.050  -0.093  -0.270  -0.009   0.180   0.180  -0.038  -0.074   0.206
## V12     -0.020   0.009   0.010   0.045   0.014   0.041  -0.012  -0.020   0.660
## V13     -0.027   0.020   0.067   0.032  -0.028  -0.053   0.022  -0.026   0.679
## V14      0.069   0.079   0.339   0.077   0.249   0.289   0.177  -0.174  -0.164
## V15      0.017  -0.086   0.326  -0.081   0.179  -0.160  -0.200   0.261   0.031
## V16     -0.086  -0.155  -0.239   0.249   0.151   0.312   0.048  -0.257  -0.098
## V17      0.000   0.172  -0.186  -0.125  -0.139  -0.225  -0.508  -0.038  -0.080
## V18     -0.013  -0.015  -0.117  -0.077   0.131   0.112  -0.520  -0.221  -0.041
## V19      0.056  -0.139   0.250   0.018   0.454   0.069  -0.272   0.226   0.010
## V20     -0.177   0.410   0.078   0.014   0.235  -0.201  -0.025  -0.082  -0.003
## V21      0.131  -0.117   0.114   0.043  -0.093  -0.337  -0.109  -0.456  -0.015
## V22     -0.227   0.191  -0.152  -0.146   0.051   0.212  -0.011   0.453   0.012
## V23     -0.106   0.092   0.170  -0.270  -0.364   0.107   0.010  -0.011  -0.004
## V24      0.164   0.345   0.299   0.138  -0.228  -0.158  -0.041   0.077  -0.010
## V25      0.034  -0.046  -0.217  -0.549   0.003   0.073   0.016  -0.045   0.024
## V26      0.108  -0.079  -0.110   0.393   0.090  -0.156   0.142  -0.026   0.006
## V27      0.134  -0.075   0.209  -0.335   0.270  -0.240   0.183  -0.143   0.041
## V28      0.167  -0.040  -0.250   0.309   0.095  -0.233  -0.201   0.351  -0.024
## Amount   0.000   0.000   0.000   0.000   0.000   0.000   0.000   0.000   0.000
## Class    0.000   0.000   0.000   0.000   0.000   0.000   0.000   0.000   0.000
##        Comp.29 Comp.30 Comp.31
## Time     0.397   0.005   0.000
## V1      -0.066  -0.054   0.034
## V2       0.006   0.046   0.081
## V3       0.247  -0.097   0.032
## V4       0.059   0.070  -0.015
## V5      -0.099  -0.051   0.058
## V6       0.037  -0.022  -0.033
## V7      -0.048  -0.096  -0.060
## V8       0.021   0.010   0.016
## V9       0.006  -0.050   0.007
## V10     -0.015  -0.112   0.015
## V11      0.142   0.082   0.000
## V12     -0.069  -0.135   0.001
## V13      0.038  -0.002  -0.001
## V14      0.061  -0.155  -0.005
## V15      0.107  -0.001   0.001
## V16     -0.004  -0.102   0.000
## V17      0.047  -0.168  -0.001
## V18     -0.051  -0.058  -0.006
## V19     -0.017   0.018   0.009
## V20      0.029   0.012  -0.051
## V21     -0.027   0.021  -0.016
## V22     -0.084  -0.001   0.010
## V23     -0.029  -0.002   0.017
## V24      0.009  -0.004  -0.001
## V25      0.135   0.003   0.007
## V26      0.024   0.003   0.001
## V27      0.003   0.009  -0.004
## V28      0.005   0.005  -0.002
## Amount   0.001  -0.002   0.145
## Class    0.009  -0.373  -0.001

Use Scree plot to pick number of PCs to use

Use scree test to find that Elbow occurs at 4 components and these 4 components are statistically significant

screeplot(PCA, type='l',main='Screeplot for credit data')
abline(1,0, col ='red',lty=2)

Another method for PCA analysis using psych package

install.packages('psych', repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Steve/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'psych' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\Steve\AppData\Local\Temp\RtmpyWFmpx\downloaded_packages
library(psych)
## 
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha

PCA for 4 PCs

PCA1 <- principal(credit_df, nfactors = 4, rotate='none')
PCA1
## Principal Components Analysis
## Call: principal(r = credit_df, nfactors = 4, rotate = "none")
## Standardized loadings (pattern matrix) based upon correlation matrix
##          PC1   PC2   PC3   PC4     h2    u2 com
## Time   -0.04  0.27 -0.88  0.00 0.8420 0.158 1.2
## V1     -0.24  0.16 -0.10  0.07 0.1000 0.900 2.4
## V2     -0.55 -0.13  0.00 -0.04 0.3173 0.683 1.1
## V3     -0.20  0.07  0.62 -0.07 0.4395 0.560 1.3
## V4      0.11 -0.20  0.08 -0.08 0.0652 0.935 2.3
## V5     -0.41  0.17 -0.17  0.09 0.2313 0.769 1.8
## V6      0.22  0.04  0.09 -0.04 0.0618 0.938 1.5
## V7      0.40  0.28 -0.05  0.08 0.2467 0.753 1.9
## V8     -0.10 -0.04  0.04 -0.03 0.0155 0.985 1.9
## V9     -0.05  0.12  0.05  0.11 0.0296 0.970 2.7
## V10    -0.11  0.27  0.05  0.06 0.0934 0.907 1.5
## V11     0.01 -0.28  0.26 -0.09 0.1562 0.844 2.2
## V12    -0.02  0.37 -0.06 -0.64 0.5441 0.456 1.6
## V13     0.01 -0.02  0.09  0.69 0.4834 0.517 1.0
## V14     0.03  0.34  0.24  0.13 0.1881 0.812 2.2
## V15     0.00 -0.06  0.24 -0.06 0.0640 0.936 1.3
## V16    -0.01  0.25  0.06  0.07 0.0684 0.932 1.3
## V17     0.00  0.37  0.22  0.11 0.1996 0.800 1.8
## V18     0.03  0.17 -0.07  0.06 0.0398 0.960 1.7
## V19    -0.06 -0.03 -0.05 -0.02 0.0071 0.993 2.8
## V20     0.35 -0.03  0.04 -0.03 0.1286 0.871 1.1
## V21     0.11 -0.03 -0.08  0.00 0.0186 0.981 2.0
## V22    -0.07  0.05 -0.18  0.05 0.0438 0.956 1.6
## V23    -0.12  0.02 -0.06 -0.01 0.0181 0.982 1.6
## V24     0.01  0.00  0.02 -0.07 0.0049 0.995 1.3
## V25    -0.04 -0.09  0.30 -0.09 0.1093 0.891 1.4
## V26     0.00 -0.02  0.05 -0.04 0.0048 0.995 2.3
## V27     0.03 -0.02  0.00  0.01 0.0015 0.999 2.0
## V28     0.01 -0.01  0.01  0.03 0.0015 0.998 1.8
## Amount  0.99  0.03 -0.03  0.00 0.9789 0.021 1.0
## Class   0.02 -0.89 -0.26  0.00 0.8611 0.139 1.2
## 
##                        PC1  PC2  PC3  PC4
## SS loadings           1.96 1.73 1.68 1.00
## Proportion Var        0.06 0.06 0.05 0.03
## Cumulative Var        0.06 0.12 0.17 0.21
## Proportion Explained  0.31 0.27 0.26 0.16
## Cumulative Proportion 0.31 0.58 0.84 1.00
## 
## Mean item complexity =  1.7
## Test of the hypothesis that 4 components are sufficient.
## 
## The root mean square of the residuals (RMSR) is  0.05 
##  with the empirical chi square  681022.4  with prob <  0 
## 
## Fit based upon off diagonal values = 0.37